package controller

import (
	"gin_web/model"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"strconv"
)

type Model interface {
	model.User
}

type CurdController interface {
	Save(ctx *gin.Context)
	Update(ctx *gin.Context)
	Get(ctx *gin.Context)
	Delete(ctx *gin.Context)
}

func PageList[M Model](ctx *gin.Context, db *gorm.DB, m *[]M) int64 {
	pageNum, _ := strconv.Atoi(ctx.DefaultQuery("pageNum", "1"))
	pageSize, _ := strconv.Atoi(ctx.DefaultQuery("pageSize", "10"))
	db.Offset((pageNum - 1) * pageSize).Limit(pageSize).Find(&m)
	var total int64
	db.Model(m).Count(&total)
	return total
}
